{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import seaborn as sns\n",
    "import numpy as np\n",
    "import matplotlib\n",
    "import matplotlib.pylab as plt\n",
    "plt.style.use('bmh')\n",
    "from sklearn.linear_model import Ridge"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_test = pd.read_csv('./test.csv')\n",
    "df_ex = pd.read_csv('./sample_submission.csv')\n",
    "df_train = pd.read_csv('./sales_train.csv')\n",
    "df_shop = pd.read_csv('./shops.csv')\n",
    "df_items = pd.read_csv('./items.csv')\n",
    "df_cat = pd.read_csv('./item_categories.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get item sells by month\n",
    "grouped = df_train.groupby(['item_id', 'shop_id', 'date_block_num'])\n",
    "month_stat = grouped['item_cnt_day'].sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date_block_num</th>\n",
       "      <th>item_cnt_m</th>\n",
       "      <th>item_id</th>\n",
       "      <th>shop_id</th>\n",
       "      <th>item_category_id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>747775</th>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>9607</td>\n",
       "      <td>46</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>830176</th>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>10904</td>\n",
       "      <td>27</td>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>321175</th>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4244</td>\n",
       "      <td>2</td>\n",
       "      <td>23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>321172</th>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4244</td>\n",
       "      <td>0</td>\n",
       "      <td>23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1292935</th>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>17114</td>\n",
       "      <td>44</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         date_block_num  item_cnt_m  item_id  shop_id  item_category_id\n",
       "747775                0         1.0     9607       46                40\n",
       "830176                0         1.0    10904       27                55\n",
       "321175                0         1.0     4244        2                23\n",
       "321172                0         1.0     4244        0                23\n",
       "1292935               0         1.0    17114       44                40"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Create train table with \"item_cnt_month\"\n",
    "X = np.array([*month_stat.index.values])\n",
    "Y = np.array(month_stat.values)\n",
    "df_train_m = pd.DataFrame({'date_block_num':X[:,2], 'item_id':X[:,0], 'shop_id':X[:,1], 'item_cnt_m':Y})\n",
    "df_train_m = pd.merge(df_train_m, df_items[['item_id', 'item_category_id']], on='item_id')\n",
    "df_train_m = df_train_m.sort_values(by='date_block_num')\n",
    "df_train_m.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>item_id</th>\n",
       "      <th>shop_id</th>\n",
       "      <th>item_category_id</th>\n",
       "      <th>date_block_num</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>747775</th>\n",
       "      <td>9607</td>\n",
       "      <td>46</td>\n",
       "      <td>40</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>830176</th>\n",
       "      <td>10904</td>\n",
       "      <td>27</td>\n",
       "      <td>55</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>321175</th>\n",
       "      <td>4244</td>\n",
       "      <td>2</td>\n",
       "      <td>23</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>321172</th>\n",
       "      <td>4244</td>\n",
       "      <td>0</td>\n",
       "      <td>23</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1292935</th>\n",
       "      <td>17114</td>\n",
       "      <td>44</td>\n",
       "      <td>40</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         item_id  shop_id  item_category_id  date_block_num\n",
       "747775      9607       46                40               0\n",
       "830176     10904       27                55               0\n",
       "321175      4244        2                23               0\n",
       "321172      4244        0                23               0\n",
       "1292935    17114       44                40               0"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Y = df_train_m['item_cnt_m']\n",
    "X0 = df_train_m[['item_id', 'shop_id', 'item_category_id', 'date_block_num']]\n",
    "X0.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.linear_model import Ridge\n",
    "from sklearn.tree import DecisionTreeRegressor\n",
    "from sklearn.ensemble import AdaBoostRegressor, BaggingRegressor\n",
    "from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor\n",
    "from sklearn.model_selection import cross_val_score \n",
    "from sklearn.preprocessing import OneHotEncoder"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "rgr_ridge = Ridge()\n",
    "dtr = DecisionTreeRegressor()\n",
    "ada = AdaBoostRegressor()\n",
    "bag = BaggingRegressor(verbose=2)\n",
    "random_forest = RandomForestRegressor(verbose=2)\n",
    "gboost = GradientBoostingRegressor(verbose=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      Iter       Train Loss   Remaining Time \n",
      "         1          71.4625            2.27m\n",
      "         2          68.7375            2.23m\n",
      "         3          66.5228            2.32m\n",
      "         4          64.7057            2.34m\n",
      "         5          63.2268            2.35m\n",
      "         6          62.0068            2.39m\n",
      "         7          60.9788            2.45m\n",
      "         8          60.1401            2.40m\n",
      "         9          59.4255            2.38m\n",
      "        10          58.8387            2.37m\n",
      "        11          58.3321            2.35m\n",
      "        12          57.9124            2.32m\n",
      "        13          57.5440            2.28m\n",
      "        14          57.3026            2.25m\n",
      "        15          57.2597            2.23m\n",
      "        16          57.0697            2.22m\n",
      "        17          57.0339            2.21m\n",
      "        18          56.9730            2.17m\n",
      "        19          56.8837            2.16m\n",
      "        20          56.8573            2.12m\n",
      "        21          56.8344            2.08m\n",
      "        22          56.7972            2.06m\n",
      "        23          56.7625            2.03m\n",
      "        24          56.7428            2.00m\n",
      "        25          56.7256            1.96m\n",
      "        26          56.7111            1.93m\n",
      "        27          56.6839            1.92m\n",
      "        28          56.6115            1.89m\n",
      "        29          56.5985            1.86m\n",
      "        30          56.5753            1.83m\n",
      "        31          56.5460            1.80m\n",
      "        32          56.4245            1.77m\n",
      "        33          56.1400            1.74m\n",
      "        34          56.1283            1.71m\n",
      "        35          56.0367            1.68m\n",
      "        36          56.0267            1.66m\n",
      "        37          56.0173            1.63m\n",
      "        38          55.9981            1.60m\n",
      "        39          55.9695            1.58m\n",
      "        40          55.7818            1.55m\n",
      "        41          55.7729            1.52m\n",
      "        42          55.7364            1.50m\n",
      "        43          55.5420            1.47m\n",
      "        44          55.5355            1.44m\n",
      "        45          55.4657            1.41m\n",
      "        46          55.4583            1.39m\n",
      "        47          55.4543            1.36m\n",
      "        48          55.4050            1.33m\n",
      "        49          55.0674            1.31m\n",
      "        50          55.0477            1.28m\n",
      "        51          54.9914            1.25m\n",
      "        52          54.9570            1.23m\n",
      "        53          54.9521            1.20m\n",
      "        54          54.9456            1.17m\n",
      "        55          54.9266            1.15m\n",
      "        56          54.8131            1.13m\n",
      "        57          54.7703            1.10m\n",
      "        58          54.7530            1.07m\n",
      "        59          54.7430            1.05m\n",
      "        60          54.7115            1.02m\n",
      "        61          54.6740           59.81s\n",
      "        62          54.6579           58.26s\n",
      "        63          54.6328           56.78s\n",
      "        64          54.6141           55.29s\n",
      "        65          54.5800           53.61s\n",
      "        66          54.5674           51.99s\n",
      "        67          54.5179           50.42s\n",
      "        68          54.4196           48.95s\n",
      "        69          54.4061           47.45s\n",
      "        70          54.3777           45.89s\n",
      "        71          54.3673           44.35s\n",
      "        72          54.3567           42.86s\n",
      "        73          54.3371           41.35s\n",
      "        74          54.3051           39.79s\n",
      "        75          54.2796           38.23s\n",
      "        76          54.2641           36.65s\n",
      "        77          54.2556           35.12s\n",
      "        78          54.0165           33.56s\n",
      "        79          53.9959           32.04s\n",
      "        80          53.9843           30.52s\n",
      "        81          53.7907           28.97s\n",
      "        82          53.7824           27.46s\n",
      "        83          53.7744           25.93s\n",
      "        84          53.7118           24.39s\n",
      "        85          53.6986           22.84s\n",
      "        86          53.6741           21.33s\n",
      "        87          53.6545           19.81s\n",
      "        88          53.6471           18.26s\n",
      "        89          53.6369           16.73s\n",
      "        90          53.6204           15.20s\n",
      "        91          53.5899           13.69s\n",
      "        92          53.5659           12.16s\n",
      "        93          53.5418           10.64s\n",
      "        94          53.5305            9.11s\n",
      "        95          53.5204            7.59s\n",
      "        96          53.5128            6.07s\n",
      "        97          53.4982            4.55s\n",
      "        98          53.4751            3.04s\n",
      "        99          53.4580            1.52s\n",
      "       100          53.4497            0.00s\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "GradientBoostingRegressor(alpha=0.9, criterion='friedman_mse', init=None,\n",
       "             learning_rate=0.1, loss='ls', max_depth=3, max_features=None,\n",
       "             max_leaf_nodes=None, min_impurity_split=1e-07,\n",
       "             min_samples_leaf=1, min_samples_split=2,\n",
       "             min_weight_fraction_leaf=0.0, n_estimators=100,\n",
       "             presort='auto', random_state=None, subsample=1.0, verbose=2,\n",
       "             warm_start=False)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gboost.fit(X0, Y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Test data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>item_cnt_month</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>0.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>0.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>0.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>0.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   ID  item_cnt_month\n",
       "0   0             0.5\n",
       "1   1             0.5\n",
       "2   2             0.5\n",
       "3   3             0.5\n",
       "4   4             0.5"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_ex.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>shop_id</th>\n",
       "      <th>item_id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>5037</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>5320</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>5233</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>5232</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>5268</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   ID  shop_id  item_id\n",
       "0   0        5     5037\n",
       "1   1        5     5320\n",
       "2   2        5     5233\n",
       "3   3        5     5232\n",
       "4   4        5     5268"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_test.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_test_a = pd.merge(df_test, df_items[['item_id', 'item_category_id']], on='item_id')\n",
    "df_test_a = df_test_a.sort_values(by='ID')\n",
    "df_test_a['date_block_num'] = [34]*len(df_test_a)\n",
    "X0_test = df_test_a[['item_id', 'shop_id', 'item_category_id', 'date_block_num']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "Y_pred = gboost.predict(X0_test)\n",
    "df_ex['item_cnt_month'] = Y_pred\n",
    "df_ex = df_ex.set_index('ID')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_ex.to_csv('pred.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
